package ca.ericslandry.server.handler;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

import org.apache.commons.logging.Log;

import ca.ericslandry.server.dao.UserDAO;
import ca.ericslandry.shared.domain.User;
import ca.ericslandry.shared.rpc.GetUser;
import ca.ericslandry.shared.rpc.GetUserResult;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.inject.Inject;

public class GetUserHandler implements ActionHandler<GetUser, GetUserResult> {
	private final Log logger;

	@Inject
	public GetUserHandler(final Log logger) {
		this.logger = logger;
	}

	@Override
	public GetUserResult execute(GetUser action, ExecutionContext context) throws ActionException {
		logger.debug("execute getuser");
		UserDAO dao = new UserDAO();
		User user = null;
		try {
			user = dao.get(action.getId());
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new GetUserResult(user);
	}

	@Override
	public Class<GetUser> getActionType() {
		return GetUser.class;
	}

	@Override
	public void rollback(GetUser action, GetUserResult result, ExecutionContext context) throws ActionException {
		// Nothing to do here
	}
}
